<template>
  <div class="course-info">
    <van-cell class="course-img">
      <img :src="course.courseImgUrl" alt="">
    </van-cell>
    <van-cell class="course-description">
      <h2 v-text="course.courseName"></h2>
      <p v-text="course.previewFirstField"></p>
      <div class="course-sale-info">
        <p class="course-price">
          <span class="discounts">￥{{ course.discounts }}</span>
          <span>￥{{ course.price }}</span>
        </p>
        <span class="tag">{{ course.sales }}人已购</span>
        <span class="tag">每周三、五更新</span>
      </div>
    </van-cell>
    <van-cell class="course-detail">
      <van-tabs sticky>
        <van-tab title="详情">
          <div v-html="course.courseDescription" class="course-description"></div>
        </van-tab>
        <van-tab title="内容">
          <course-section
            v-for="item in sections"
            :key="item.id"
            :section-data="item"
          ></course-section>
        </van-tab>
      </van-tabs>
    </van-cell>
    <van-tabbar>
      <div class="price">
        <span v-text="course.discountsTag"></span>
        <span class="discounts">￥{{ course.discounts }}</span>
        <span>￥{{ course.price }}</span>
      </div>
      <van-button type="primary" @click="handlePlay">立即购买</van-button>
    </van-tabbar>
  </div>
</template>

<script>
import { getCourseById, getSectionAndLesson } from '@/services/course'
import CourseSection from './components/CourseSection'
export default {
  name: 'CourseInfo',
  props: {
    courseId: {
      type: [String, Number],
      required: true
    }
  },
  data () {
    return {
      course: {},
      sections: {}
    }
  },
  components: {
    CourseSection
  },
  created () {
    this.loadCourse()
    this.loadSections()
  },
  methods: {
    async loadCourse () {
      const { data } = await getCourseById({
        courseId: this.courseId
      })
      // console.log(data)
      this.course = data.content
    },
    async loadSections () {
      const { data } = await getSectionAndLesson({
        courseId: this.courseId
      })
      this.sections = data.content.courseSectionList
    },
    handlePlay () {
      if (this.$store.state.user) {
        this.$router.push({
          name: 'pay',
          params: {
            courseId: this.courseId
          }
        })
      } else {
        this.$router.push({
          name: 'login',
          query: {
            redirect: this.$route.fullPath
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.van-cell {
  padding: 0;
}

.course-info {
  padding-bottom: 50px;
}

.course-img {
  height: 200px;

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
  }
}

.course-description {
  padding: 10px 20px;
  // height: 150px;
}

.course-sale-info {
  display: flex;

  .course-price {
    flex: 1;
    margin: 0;
  }

  .tag {
    background: #f8f9fa;
    font-size: 12px;
    font-weight: 700;
    color: #666;
    margin-left: 10px;
    padding: 7px;
    line-height: 15px;
    border-radius: 5px;
  }
}
.discounts {
  color: #ff7452;
  font-size: 24px;
  font-weight: 700;
}

.van-tabbar {
  line-height: 50px;
  padding: 0 20px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  font-size: 14px;
}
</style>
